package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BusinessSalesMonth;
import cn.stylefeng.guns.sys.modular.system.mapper.BusinessSalesMonthMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BusinessSalesMonthParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BusinessSalesMonthResult;
import cn.stylefeng.guns.sys.modular.system.service.BusinessSalesMonthService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-06-04
 */
@Service
public class BusinessSalesMonthServiceImpl extends ServiceImpl<BusinessSalesMonthMapper, BusinessSalesMonth> implements BusinessSalesMonthService {
    private Logger logger = LoggerFactory.getLogger(BusinessMonthServiceImpl.class);
    @Override
    public void add(BusinessSalesMonthParam param){
        BusinessSalesMonth entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BusinessSalesMonthParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BusinessSalesMonthParam param){
        BusinessSalesMonth oldEntity = getOldEntity(param);
        BusinessSalesMonth newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BusinessSalesMonthResult findBySpec(BusinessSalesMonthParam param){
        return null;
    }

    @Override
    public List<BusinessSalesMonthResult> findListBySpec(BusinessSalesMonthParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BusinessSalesMonthParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertData(String month, List<Map<String, Object>> dataList) {

        if (CollectionUtil.isNotEmpty(dataList)) {
            for (Map<String, Object> data : dataList) {
                BusinessSalesMonth entity = new BusinessSalesMonth();
                entity.setShopId(Integer.parseInt(data.get("shopId").toString()));
                entity.setShopCode(data.get("shopCode").toString());
                entity.setShopName(data.get("shopName").toString());
                entity.setBigClassId(Long.parseLong(data.get("bigClassId").toString()));
                entity.setBigClassName(data.get("bigClassName").toString());
                entity.setSmallClassId(Long.parseLong(data.get("smallClassId").toString()));
                entity.setSmallClassName(data.get("smallClassName").toString());
                entity.setItemCode(data.get("itemCode").toString());
                entity.setItemId(Long.parseLong(data.get("itemId").toString()));
                entity.setItemName(data.get("itemName").toString());
                entity.setUnitName(data.get("unitName").toString());
                entity.setStdPrice(new BigDecimal(data.get("stdPrice").toString()));
                entity.setpName(data.get("pname").toString());
                entity.setSizeName(data.get("sizeName").toString());
                entity.setSizeId(Long.parseLong(data.get("sizeId").toString()));
                entity.setLastQty(new BigDecimal(data.get("lastQty").toString()));
                entity.setOrigMoney(new BigDecimal(data.get("origMoney").toString()));

                String monthStr = month.replace("-", "");
                String id = monthStr + "" + entity.getShopId();
                entity.setId(Long.parseLong(id));
                entity.setMonthDate(month);
                try {
                    //先使用此方法，同一店每一月应该只有一条统计数据
                    this.saveOrUpdate(entity);
                } catch (Exception e) {
                    logger.error("定时任务失败，录入门店主食数据，失败-统计{}月就餐人数失败", month);
                    e.printStackTrace();
                }
            }
        }

    }

    private Serializable getKey(BusinessSalesMonthParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BusinessSalesMonth getOldEntity(BusinessSalesMonthParam param) {
        return this.getById(getKey(param));
    }

    private BusinessSalesMonth getEntity(BusinessSalesMonthParam param) {
        BusinessSalesMonth entity = new BusinessSalesMonth();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
